<?php
/**
 * User:chendenan
 * Created On 2022/3/17 0017 22:25
 */
namespace app\common\lib;
class Arr{
    /**
     * 返回所有子极分类 组合成一维数组
     * @param $arr
     * @param $id
     * @return array
     */
    public static function list_to_tree($arr, $id = null) {
        $pid = $arr[0]['pid'];
        $arr_assert = []; //辅助数组
        $arr_new = [];
        foreach ($arr as $value) {
            $arr_new[$value['id']] = $value;
            $arr_new[$value['id']]['num'] = 1; //自己及其子代的数量
            if (!isset($arr_assert[$value['pid']])) {
                $arr_assert[$value['pid']]['start'] = 0;
                $arr_assert[$value['pid']]['end'] = 0;
            }
            $arr_assert[$value['pid']][] = $value['id'];
            ++$arr_assert[$value['pid']]['end'];
        }
        $result = []; //返回的结果集
        $count = count($arr_new);
        $offset = 0;
        $level = 0; //显示层级
        while ($offset < $count) {
            if (isset($arr_assert[$pid]) && $arr_assert[$pid]['start'] < $arr_assert[$pid]['end']) {
                $index = $arr_assert[$pid]['start'];
                ++$arr_assert[$pid]['start'];
                $pid = $arr_assert[$pid][$index];
                $arr_new[$pid]['offset'] = $offset;
                $arr_new[$pid]['level'] = $level;
                $result[$pid] = &$arr_new[$pid];
                ++$level;
                ++$offset;
            } else {
                --$level;
                $num = $arr_new[$pid]['num'];
                $pid = $arr_new[$pid]['pid'];
                if ($pid !== 0)
                    $arr_new[$pid]['num'] += $num;
            }
        }
        if ($id)
            return array_slice($result, $arr_new[$id]['offset'], $arr_new[$id]['num']); //返回某个id的自身及其所有子代的记录
        return $result;
    }
    /**
     * 分类树 支持无限极分类
     * @param $data
     * @return array
     */
    public static function getTree($data){
        $items = [];
        foreach ($data as $v){
            $items[$v['id']] = $v;
        }
        $tree = [];
        foreach ($items as $id=>$item){
            if(isset($items[$item['pid']])){
                $items[$item['pid']]['list'][] = $items[$id];
            }else{
                $tree[] = &$items[$id];
            }
        }
        return $tree;
    }
    /**
     * 截取数组中的个数
     * @param $data
     * @param int $fistCount 一级分类
     * @param int $secondCount 二级分类
     * @param int $threeCount 三级分类
     * @return array
     */
    public static function sliceTreeArr($data,$fistCount = 5,$secondCount = 3 , $threeCount = 5){
        $data = array_slice($data,0,$fistCount);
        foreach ($data as $k => $v){
            if (!empty($v['list'])){
                $data[$k]['list'] = array_slice($v['list'],0,$secondCount);
                foreach ($v['list'] as $kk => $vv){
                    if (!empty($vv['list'])){
                        $data[$k]['list'][$kk]['list'] = array_slice($vv['list'],0,$threeCount);
                    }
                }
            }
        }
        return $data;
    }

    /**
     * 没有分页数据 默认返回的分页数据
     * @param $num
     * @return array
     */
    public static function getPaginateDefaultData($num){
        $result = [
            "total" => 0,
            "per_page" => $num,
            "current_page" => 1,
            "last_page" =>0,
            "data" => [],
        ];
        return $result;
    }
    public static function arrSortByKey($result,$key,$sort = SORT_DESC){
        if(!is_array($result) || !$key){
            return [];
        }
        array_multisort(array_column($result,$key),$sort,$result);
        return $result;
    }

}